<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Menu.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Menu.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Menu.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_MenuController extends Zend_Controller_Action {
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelMenu;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelMenu = new End_Models_Menu();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }
    
    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {

        $form = new Application_Form_Menu();

        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $name = $form->getValue('name');
                $description = $form->getValue('description');
                $user_id = (int) $form->getValue('user_id');
                $user_name = $form->getValue('user_name');
                $created_date = $form->getValue('created_date');
                $language = $form->getValue('language');

                $menu = new Menu(null, $name, $description, $user_id, $user_name, $created_date, $language);

                $this->modelMenu->_insert($menu);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $menu_id = (int) $this->_getParam('menu_id');
                $this->modelMenu->_delete($menu_id);
            }
            $this->_helper->redirector('index');
        } else {
            $menu_id = $this->_getParam('menu_id', 0);
            $this->view->Menu = Utility_Converter::objectToArray($this->modelMenu->_find($menu_id));
        }
    }

    public function editAction() {
        $menu_id = $this->_getParam('menu_id');
        $form = new Application_Form_Menu();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $modified_date = date('Y-m-d H:i:s');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }

            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $name = $form->getValue('name');
                $description = $form->getValue('description');
                $user_id = (int) $form->getValue('user_id');
                $user_name = $form->getValue('user_name');
                $created_date = $form->getValue('created_date');
                $language = $form->getValue('language');
                $menu = new Menu($menu_id, $name, $description, $user_id, $user_name, $created_date, $language);
                $this->modelMenu->_update($menu);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $menu_id = (int) $this->_getParam('menu_id', 0);
            if ($menu_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelMenu->_find($menu_id)));
            }
        }
    }

    public function indexAction() {
        $menu = $this->modelMenu->fetchAll();
        $this->view->Menu = $menu;
    }

}

